﻿//
// GNS_DailyTop.cpp
// MobileManageSys
//FuncTion  每日充值
// Created by mr.l on 16/1/16.
//
//
#include "GnsClientFrame.h"
#include "GNS_DailyTop.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
//extern ISocketMgr * _GameSock;
GNS_DailyTop::GNS_DailyTop()
{
    pGNS_DailyTop=0;
    TimeText = nullptr;
    ShowText = nullptr;
    pCalendar=nullptr;
    TypeListView=nullptr;
    DateListView=nullptr;
    typeNum=0;
    Mon=0;
    Year=0;
    Day=0;
}

GNS_DailyTop::~GNS_DailyTop()
{
    
}

GNS_DailyTop* GNS_DailyTop::createLayer()
{
    auto pGNS_DailyTop = GNS_DailyTop::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_DailyTop);
    return pGNS_DailyTop;
}

bool GNS_DailyTop::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    /**
     *  @author MR.L, 16-02-16 11:02:50
     *
     *  获取当前时间
     */
    time_t tt;
    time(&tt);
    struct tm * time;
    time = localtime(&tt);
    
    Mon = (int)time->tm_mon+1;
    Year = time->tm_year+1900;
    Day  =(int)time->tm_mday;
    
    return true;
}
void GNS_DailyTop::SetNodeName(std::string CsbName)
{
    pGNS_DailyTop = CSLoader::createNode(CsbName);
    pGNS_DailyTop->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_DailyTop);
    addChild(pGNS_DailyTop,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_DailyTop);
    auto pBackBtn = static_cast<Button*>(pGNS_DailyTop->getChildByName("DailyTopBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyTop::BtnCallBack, this));
    
    TimeText = static_cast<Text*>(pGNS_DailyTop->getChildByName("Text_3"));
    TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));
    
    ShowText = static_cast<Text*>(pGNS_DailyTop->getChildByName("Text_5"));
    
    auto pTimeBtn  = static_cast<Button*>(pGNS_DailyTop->getChildByName("Button_3"));
    pTimeBtn->setTag(2);
    pTimeBtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyTop::BtnCallBack, this));
    
    auto typebtn = static_cast<Button*>(pGNS_DailyTop->getChildByName("typebtn"));
    typebtn->setTag(3);
    typebtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyTop::BtnCallBack, this));
    
    auto pTimeSer = static_cast<Button*>(pGNS_DailyTop->getChildByName("Button_8"));
    pTimeSer->setTag(2);
    pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_DailyTop::BtnCallBack, this));
    
    auto typrbtnSrt = static_cast<Button*>(pGNS_DailyTop->getChildByName("Button_8_0"));
    typrbtnSrt->setTag(3);
    typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_DailyTop::BtnCallBack, this));
    
    
    TypeListView = static_cast<ListView*>(pGNS_DailyTop->getChildByName("TypeListView"));
    TypeListView->setVisible(false);
    TypeListView->setZOrder(13);
    DateListView = static_cast<ListView*>(pGNS_DailyTop->getChildByName("ListView_1"));
    
    auto TypeAllBtn = static_cast<Button*>(TypeListView->getChildByName("Allbtn"));
    TypeAllBtn->setTag(5);
    TypeAllBtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyTop::ListViewCallBack, this));
    
    auto TypeAgentBtn = static_cast<Button*>(TypeListView->getChildByName("AgentBtn"));
    TypeAgentBtn->setTag(6);
    TypeAgentBtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyTop::ListViewCallBack, this));
    
    auto TypePromoterBtn = static_cast<Button*>(TypeListView->getChildByName("PromoterBtn"));
    TypePromoterBtn->setTag(7);
    TypePromoterBtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyTop::ListViewCallBack, this));
    
    typeNum= GNS_PlayerMgr::getInstance()->getUserLevel();
    if (typeNum==1) {
        TypeListView->removeChild(TypeAllBtn);
        ShowText->setString(TypeAgentBtn->getTitleText());
    }else if (typeNum==2)
    {
        TypeListView->removeChild(TypeAllBtn);
        TypeListView->removeChild(TypeAgentBtn);
        ShowText->setString(TypePromoterBtn->getTitleText());
    }
    
}
void GNS_DailyTop::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
                //auto RevRepNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(3);
                //RevRepNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("RevRep", true);
                break;
            }
            case 2:
            {
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_DailyTop::TimeButtonCallBack, this));
                /*pCalendar =GNS_Calendar::createCalendar();
                pCalendar->SetCalendBgImage("Calendarbg.png");
                std::vector<Button *> CalenderBtn;
                pCalendar->GetTimeButton()->swap(CalenderBtn);
                for (auto & it:CalenderBtn) {
                    it->setTag(100);
                    it->addTouchEventListener(CC_CALLBACK_2(GNS_DailyTop::TimeButtonCallBack, this));
                }*/

                break;
            }
            case 3:
            {
                if (TypeListView->isVisible()==true) {
                    TypeListView->setVisible(false);
                }else
                {
                    TypeListView->setVisible(true);
                }
                break;
            }
            default:
                break;
        }
    }
}
void GNS_DailyTop::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        Mon = pCalendar->Mon;
        Year = pCalendar->year;
        Day = atoi(BtnText.c_str());
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
        GNS_PlayerMgr::getInstance()->getDialyToInfo()->clear();
        CMD_CS_GetDialyExchangeInfo pGetDialyInfo;
        pGetDialyInfo._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetDialyInfo._type = typeNum;
        pGetDialyInfo._operType=1;
        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
        memcpy(pGetDialyInfo._selDate, TimeDate.c_str(), 11);
        pGetDialyInfo._type = GNS_PlayerMgr::getInstance()->getUserLevel();
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_DIALY_EXCHANGE_INFO, (char*)&pGetDialyInfo, sizeof(pGetDialyInfo));
    }
}
void GNS_DailyTop::ListViewCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int Btntag = pButton->getTag();
        std::string btntext = pButton->getTitleText();
        ShowText->setString(btntext);
        TypeListView->setVisible(false);
        switch (Btntag) {
            case 5:
            {
                GNS_PlayerMgr::getInstance()->getDialyToInfo()->clear();
                CMD_CS_GetDialyExchangeInfo pGetDialyInfo;
                pGetDialyInfo._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                pGetDialyInfo._type = 0;
                pGetDialyInfo._operType=1;
                std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                memcpy(pGetDialyInfo._selDate, TimeDate.c_str(), 11);
                pGetDialyInfo._type = GNS_PlayerMgr::getInstance()->getUserLevel();
                GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_DIALY_EXCHANGE_INFO, (char*)&pGetDialyInfo, sizeof(pGetDialyInfo));

                typeNum=0;
                break;
            }
            case 6:
            {
                GNS_PlayerMgr::getInstance()->getDialyToInfo()->clear();
                CMD_CS_GetDialyExchangeInfo pGetDialyInfo;
                pGetDialyInfo._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                pGetDialyInfo._type = 1;
                pGetDialyInfo._operType=1;
                std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                memcpy(pGetDialyInfo._selDate, TimeDate.c_str(), 11);
                GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_DIALY_EXCHANGE_INFO, (char*)&pGetDialyInfo, sizeof(pGetDialyInfo));
                typeNum=1;
                break;
            }
            case 7:
            {
                GNS_PlayerMgr::getInstance()->getDialyToInfo()->clear();
                CMD_CS_GetDialyExchangeInfo pGetDialyInfo;
                pGetDialyInfo._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                pGetDialyInfo._type = 2;
                pGetDialyInfo._operType=1;
                std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                memcpy(pGetDialyInfo._selDate, TimeDate.c_str(), 11);
//                pGetDialyInfo._type = GNS_PlayerMgr::getInstance()->getUserLevel();
                //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_GET_DIALY_EXCHANGE_INFO, &pGetDialyInfo, sizeof(pGetDialyInfo));
                GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_DIALY_EXCHANGE_INFO, (char*)&pGetDialyInfo, sizeof(pGetDialyInfo));
                typeNum=2;
                break;
            }
            default:
                break;
        }
    }
}

void GNS_DailyTop::AcceptSeverDate()
{
    DateListView->removeAllItems();
    Size bgSize;
    int x =0;
    for (auto it:(*GNS_PlayerMgr::getInstance()->getDialyToInfo())) {
        auto itembg = ImageView::create("line2.png");
        DateListView->addChild(itembg);
        bgSize = itembg->getContentSize();
        
        auto SendName = Text::create(StringUtils::format("%d",it._sendCode), GameFontName, 24);
        SendName->setPosition(Vec2(bgSize.width*0.143, bgSize.height*0.26));
        SendName->setColor(Color3B::BLACK);
        itembg->addChild(SendName);
        
        auto AllGold = Text::create(StringUtils::format("%lld",it._gold), GameFontName, 24);
        AllGold->setPosition(Vec2(bgSize.width*0.486, bgSize.height*0.26));
        AllGold->setColor(Color3B::BLACK);
        itembg->addChild(AllGold);
        
        auto AccepName = Text::create(StringUtils::format("%d",it._revCode), GameFontName, 24);
        AccepName->setPosition(Vec2(bgSize.width*0.829, bgSize.height*0.26));
        AccepName->setColor(Color3B::BLACK);
        itembg->addChild(AccepName);
        x++;
    }
    
    DateListView->setInnerContainerSize(Size(bgSize.width,(bgSize.height+10)*x));
    
}